#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib,urllib2,re,os,xbmc

#author:
#elypter
#-mail:elypter@yahoo.de
#




def check(url):
	#not used atm
	if url.rfind("movshare"):
		return 1
	else:
		return 0
def get_regexp():
	#return "(http\://www\.movshare\.net/embed/[^<>/]*)/\?"
	return "http\://(?:www|stream)?\.movshare\.net/(?:video|embed)/([0-9a-z]*)"
def get_name():
	return "movshare"
def join(id):
	return "http://www.movshare.net/embed/"+id
def get_link1(url):
	
	#takes an videopage-url of megavideo
	#
	#returnes the service/mirror name, the url that will be passed over to get_link2, the url of the verification code
	
	req = urllib2.Request(url)
	req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
	response = urllib2.urlopen(req)
	page=response.read()
	response.close()
	page=page.lower()
	
	if (page.rfind("we need you to prove you're human")):
		values = {'submit.x': '66',
				'submit.y' : '23'} 
		data = urllib.urlencode(values)
		req = urllib2.Request(url)
		req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
		response = urllib2.urlopen(req)
		page=response.read()
		response.close()
		page=page.lower()
	
	#print page
	
	#link = re.compile('(http\://stream\.movshare\.net/[0-9a-z]*\.avi\?[0-9a-z]*\&ec\_rate\=[0-9]*)').findall(page)
	link = re.compile('(http\://stream\.movshare\.net/[0-9a-z]*\.avi\?[0-9a-z]*)\&').findall(page)
	
	if len(link)>0:
		#return ('movshare',link[0],'')
		return ("got",link[0],'','','','','movshare')
	else:
		#return ('movashare','','')
		return ("fail",'','','','','','movshare')
	
	#java code
	#requestFileInformation(url)
	#	if (br.containsHTML("We need you to prove you're human")) {
	#		Form IAmAHuman = br.getForm(0);
	#		br.submitForm(IAmAHuman);
	#	}
	#	
	#	String dllink = br.getRegex(Pattern.compile("<embed src=\"(.*?)\" width")).getMatch(0);
	#	if (dllink == null) 
	#		dllink = br.getRegex(Pattern.compile("video/divx\" src=\"(.*?)\"  id=\"embedm")).getMatch(0);
	#	if (dllink == null) 
	#		dllink = br.getRegex(Pattern.compile("addVariable\\(\"file\",\"(http://.*?)\"")).getMatch(0);
	#	
	#	return dllink[0]
	
def get_link2(param,code):
	#should take the url that get_link1 returned
	#
	#returnes the video url
	return ("got",param,'','','','','movshare')
	
